package com.j2ee.spring.aop.privilege.aspect;

import java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;

import com.j2ee.spring.aop.privilege.annotation.PrivilegeAnnotationParse;
import com.j2ee.spring.aop.privilege.bean.Privilege;

public class PrivilegeAspect {
	
	//该集合存放用户能够访问到的权限
	private List<Privilege> privileges = new ArrayList<Privilege>() ;
	
	public List<Privilege> getPrivileges() {
		return privileges;
	}

	public void accessMethod(ProceedingJoinPoint joinPoint) throws Throwable{
		/**
		 * 1、获取到目标方法的权限
		 */
		Class targetClass = joinPoint.getTarget().getClass() ;
		String methodname = joinPoint.getSignature().getName() ;
		String privilegeName = PrivilegeAnnotationParse.parse(targetClass, methodname) ;
		
		/**
		 * 判断用户是否有权限访问目标方法
		 */
		boolean flag = false ;
		for (Privilege privilege : privileges) {
			if(privilege.getName().equals(privilegeName)){
				flag = true ; //有权限访问
				break ;
			}
		}
		if(flag) {//有权限访问
			joinPoint.proceed() ;
		} else {//没有权限访问
			System.out.println("没有访问权限");
		}
	}

}
